// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,
    ctor(){
        this.textList = [
            '我要发弹幕',
            '心态崩了啊',
            '我能怎么办啊，我也很绝望啊',
            '太秀了！',
            '我能怎么办啊，我也很绝望啊，太秀了',
            '心态崩了啊',
            '心态崩了啊',
            '心态崩了啊',
        ];
        this._showDm = true;
    },
    properties: {
        //input框
        dmInput: cc.Node,
        //发送按钮
        sendBtn: cc.Node,
        //快捷文本
        fastText: cc.Prefab,
        //内容区域
        scrollCont: cc.Node,
        //弹幕按钮
        dmBtn: cc.Node,
        //快捷语按钮
        fastBtn: cc.Node,
        //快捷语弹窗
        fastAlertBox: cc.Node,
        //输入框
        editBox: cc.EditBox,
        //是否显示弹幕
        showDm: {
            get: function(){
                return this._showDm;
            },
            set: function(val){
                this._showDm = val;
                this.dmList.active = val;
            },
        },
        //弹幕列表
        dmList: cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var _this = this;
        this.dmInput.on('edit-did-began', (editbox, customEventData)=>{
            console.log(editbox)
        })
        this.initTextList();
        this.fastAlertBox.active = false;
        this.fastAlertBox.on('touchstart', function(event){
            event.stopPropagation()
        })
        cc.find('Canvas').on('touchstart', function(){
            _this.fastAlertBox.active = false;
            _this.editBox.blur();
        })
        this.fastBtn.on('touchstart', (event)=>{
            this.fastAlertBox.active = !this.fastAlertBox.active;
            event.stopPropagation()
        })
        //发送按钮
        this.sendBtn.on('touchstart', ()=>{
            _this.node.emit('sendDm', {
                text: this.editBox.string,
                isSelf: true,
            })
            this.editBox.string = '';
            _this.editBox.blur();            
        })
        //弹幕开关
        this.dmBtn.on('touchstart', ()=>{
            this.showDm = !this.showDm
        })
    },
    initEvent(node){
        let startTime = 0;
        node.on('touchstart', ()=>{
            startTime = Date.now();
        })
        node.on('touchend', (event)=>{
            let disTime = Date.now() - startTime
            if(disTime < 200){
                let richNode = event.currentTarget.getChildByName('rich');
                let richLabel = richNode.getComponent(cc.RichText);
                this.node.emit('sendDm', {
                    text: richLabel.string,
                    isSelf: true,
                })
                this.fastAlertBox.active = false;
            }
        })

    },
    initTextList(){
        let h = 0;
        let prevT = 0;
        let arr = [];
        for(let i=0; i<this.textList.length; i++){
            let itemTextNode = cc.instantiate(this.fastText);
            let richNode = itemTextNode.getChildByName('rich');
            let richText = richNode.getComponent(cc.RichText);
            richText.string = this.textList[i];
            arr.push(itemTextNode)
            this.scrollCont.addChild(itemTextNode)
            itemTextNode.on('touchstart')
            itemTextNode.y = -prevT;
            itemTextNode.height = richNode.height;
            h+=richNode.height;
            prevT = h;
            this.initEvent(itemTextNode)
        }
        this.scrollCont.height = h;
    },
    start () {

    },

    // update (dt) {},
});
